using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Reflection;
using System.Text;

namespace Soyuz5.Utility
{
    public static class Enums
    {
        static public List<KeyValuePair<int, string>> ConvertToList(Type enumType)
        {
            List<KeyValuePair<int, string>> list = new List<KeyValuePair<int, string>>();
            foreach (object value in Enum.GetValues(enumType))
            {
                KeyValuePair<int, string> item = new KeyValuePair<int, string>(Convert.ToInt32(value), Enum.GetName(enumType, value));
                list.Add(item);
            }
            return list;
        }

        /// <summary>
        /// Uses Description attribute to pull value name.
        /// </summary>
        /// <param name="enumType"></param>
        /// <returns></returns>
        static public List<KeyValuePair<int, string>> ConvertToListWithDescriptions(Type enumType)
        {
            List<KeyValuePair<int, string>> list = new List<KeyValuePair<int, string>>();
            foreach (object value in Enum.GetValues(enumType))
            {
                KeyValuePair<int, string> item = new KeyValuePair<int, string>(Convert.ToInt32(value),
                                                                               GetEnumDescription(enumType, value));
                list.Add(item);
            }
            return list;
        }

        static public List<KeyValuePair<int, string>> ConvertToList<T>()
        {
            List<KeyValuePair<int, string>> list = new List<KeyValuePair<int, string>>();
            foreach (string value in ConvertEnumToStringArray(typeof(T)))
            {
                KeyValuePair<int, string> item = new KeyValuePair<int, string>(Convert.ToInt32(ConvertStringToEnum<T>(value)), value);
                list.Add(item);
            }
            return list;
        }

        static public T ConvertIntegerToEnum<T>(int value)
        {
            return (T)Enum.ToObject(typeof(T), value);
        }

        static public string ConvertIntegerToString<T>(int value)
        {
            T enumValue = ConvertIntegerToEnum<T>(value);
            return Enum.GetName(typeof(T), enumValue);
        }

        static public string[] ConvertEnumToStringArray(Type value)
        {
            return Enum.GetNames(value);
        }

        static public T ConvertStringToEnum<T>(string value)
        {
            return (T)Enum.Parse(typeof(T), value);
        }

        /// <summary>
        /// Gets Description attribute value for an enum value if available.
        /// </summary>
        static public string GetEnumDescription(object value)
        {
            return GetEnumDescription(value.GetType(), value);
        }

        /// <summary>
        /// Gets Description attribute value for an enum value if available.
        /// </summary>
        /// <param name="enumType"></param>
        /// <returns></returns>
        static public string GetEnumDescription(Type enumType, object value)
        {
            if (!enumType.IsEnum)
                throw new ArgumentOutOfRangeException("enumType");

            string name = null;
            MemberInfo[] members = enumType.GetMember(Enum.GetName(enumType, value));
            if (members.Length > 0)
            {
                DescriptionAttribute attr = (DescriptionAttribute) Attribute.GetCustomAttribute(
                    members[0], typeof (DescriptionAttribute));
                name = attr.Description;
            }
            if (string.IsNullOrEmpty(name))
                name = Enum.GetName(enumType, value);

            return name;
        }

        /// <summary>
        /// Converts enum description obtained using <see cref="GetEnumDescription"/> to an enum type
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="value"></param>
        /// <returns></returns>
        static public T ConvertDescriptionToEnum<T>(string value)
        {
            return (T)Enum.Parse(typeof(T), value);
        }
    }
}